package com.deng.test.controller;

import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

/**
 * 文件操作相关
 *
 * @author deng
 * @version 1.0
 * @since 2025/8/7 10:41
 */
@RestController
public class FileController {

    private final MinioClient client;

    public FileController(MinioClient client) {
        this.client = client;
    }

    @Value("${minio.bucket-name}")
    private String bucketName;

    @RequestMapping("/save")
    public String save(MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            client.putObject(
                    PutObjectArgs.builder()
                            .bucket(bucketName)
                            .object(file.getOriginalFilename())
                            .stream(inputStream, inputStream.available(), -1)
                            .contentType(file.getContentType())
                            .build()
            );
            return "successfully!";
        } catch (Exception e) {
            e.printStackTrace();
            return "Failure!";
        }
    }

    /**
     * 下载文档
     *
     * @return
     */
    @RequestMapping("/get")
    public String get() {
        return "";
    }
}
